package com.jinjiafeng.baselibrary.widget.navigationbar;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author jinjiafeng
 *         Time :18-4-22
 */

public abstract class BaseNavigationBar<P extends BaseNavigationBar.BaseNavigationParams>
        implements INavigationBar {
    protected final P mParams;

    protected BaseNavigationBar(P params) {
        this.mParams = params;
        createAndBindView();
    }

    private void createAndBindView() {
        if (mParams.mViewGroup == null) {
            //当设置的根本局为relativelayout时也能正常显示
            final ViewGroup decorView = (ViewGroup) ((Activity) mParams.mContext).getWindow().getDecorView();
            mParams.mViewGroup = (ViewGroup) decorView.getChildAt(0);
        }

        if (mParams.mViewGroup == null) {
            return;
        }

        final View view = LayoutInflater.from(mParams.mContext).inflate(bindLayoutId(),
                mParams.mViewGroup, false);
        mParams.mViewGroup.addView(view, 0);
        applyView();
    }

    public abstract static class BaseBuilder {

        private final Context mContext;
        private ViewGroup mViewGroup;

        protected BaseBuilder(Context context, ViewGroup viewGroup) {
            mContext = context;
            mViewGroup = viewGroup;
        }

        public BaseBuilder(Context context) {
            mContext = context;
        }

        /**
         * 构建NavigationBar
         *
         * @return 构建NavigationBar
         */
        public abstract BaseNavigationBar build();

    }

    public static class BaseNavigationParams {

        public final Context mContext;
        public ViewGroup mViewGroup;


        protected BaseNavigationParams(Context context, ViewGroup viewGroup) {
            mContext = context;
            mViewGroup = viewGroup;
        }
    }
}
